package net.itren.jetpackdemo.utils.network

import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import java.io.File
import java.io.Serializable
import java.net.URLConnection
import java.util.*

class HttpParams : Serializable {
    /**
     * 普通的键值对参数
     */
    var urlParamsMap: LinkedHashMap<String, String>? = null

    /**
     * 文件的键值对参数
     */
    var fileParamsMap: LinkedHashMap<String, FileWrapper>? = null

    constructor() {
        init()
    }

    constructor(key: String?, value: String?) {
        init()
        put(key, value)
    }

    constructor(key: String?, file: File) {
        init()
        put(key, file)
    }

    private fun init() {
        urlParamsMap = LinkedHashMap()
        fileParamsMap = LinkedHashMap()
    }

    fun put(params: HttpParams?) {
        if (params != null) {
            if (params.urlParamsMap != null && !params.urlParamsMap!!.isEmpty()) {
                urlParamsMap!!.putAll(params.urlParamsMap!!)
            }
            if (params.fileParamsMap != null && !params.fileParamsMap!!.isEmpty()) {
                fileParamsMap!!.putAll(params.fileParamsMap!!)
            }
        }
    }

    fun put(params: Map<String?, String?>?) {
        if (params == null || params.isEmpty()) {
            return
        }
        for ((key, value) in params) {
            put(key, value)
        }
    }

    fun put(key: String?, value: String?) {
        if (key != null && value != null) {
            urlParamsMap!![key] = value
        }
    }

    fun put(key: String?, fileWrapper: FileWrapper?) {
        if (key != null && fileWrapper != null) {
            put(key, fileWrapper.file, fileWrapper.fileName, fileWrapper.contentType)
        }
    }

    @JvmOverloads
    fun put(
        key: String?,
        file: File,
        fileName: String = file.name,
        contentType: MediaType? = guessMimeType(fileName)
    ) {
        if (key != null) {
            fileParamsMap!![key] = FileWrapper(file, fileName, contentType)
        }
    }

    fun removeUrl(key: String) {
        urlParamsMap!!.remove(key)
    }

    fun removeFile(key: String) {
        fileParamsMap!!.remove(key)
    }

    fun remove(key: String) {
        removeUrl(key)
        removeFile(key)
    }

    fun clear() {
        urlParamsMap!!.clear()
        fileParamsMap!!.clear()
    }

    private fun guessMimeType(path: String?): MediaType? {
        var path = path
        val fileNameMap = URLConnection.getFileNameMap()
        path = path!!.replace("#", "") //解决文件名中含有#号异常的问题
        var contentType = fileNameMap.getContentTypeFor(path)
        if (contentType == null) {
            contentType = "application/octet-stream"
        }
        return contentType.toMediaTypeOrNull();
    }



    override fun toString(): String {
        val result = StringBuilder()
        for ((key, value) in urlParamsMap!!) {
            if (result.length > 0) {
                result.append("&")
            }
            result.append(key).append("=").append(value)
        }
        for ((key, value) in fileParamsMap!!) {
            if (result.length > 0) {
                result.append("&")
            }
            result.append(key).append("=").append(value)
        }
        return result.toString()
    }

    companion object {
        private const val serialVersionUID = 7369819159227055048L
    }
}